import json
from datetime import date as Date
import copy

def main():
    with open('foreign.csv','r') as fr:
        all = fr.read().split('\n')
    data = all[1:]
    header = all[0]
    print(header)
    # print(data)

    headerTitles = header.split(',')

    dailyCount = 0
    lastRecDateName = ''

    allDates = {}
    allRegions = {}
    for rec in data:
        # print(rec)
        elems = rec.split(',')

        thisDate = elems[0]
        if thisDate in list(allDates.keys()):
            allDates[thisDate].append(elems)
        else:
            allDates[thisDate] = []
            allDates[thisDate].append(elems)

        thisRegion = elems[4]
        if thisRegion in list(allRegions.keys()):
            allRegions[thisRegion].append(elems)
        else:
            allRegions[thisRegion] = []
            allRegions[thisRegion].append(elems)
        # for i in range(0, len(elems)):
        #     print(headerTitles[i], elems[i])
        # print('\n')
    # for k, v in allDates.items():
        # print(k, v)

    cn = 0
    StartDate = Date(2020,1,22)
    newAllRegions = {}
    for k, v in allRegions.items():
        earliestDateThisRegion = ''
        earliestDateSeqThisRegion = 1000

        newDailyInfo = []
        for dailyInfo in v:
            thisDate = [int(x) for x in dailyInfo[0].split(' ')[0].split('/')]
            thisDateObj = Date(thisDate[0], thisDate[1], thisDate[2])
            diff = (thisDateObj - StartDate).days
            dailyInfo.append(diff)

            mainInfo = [dailyInfo[3], dailyInfo[7], dailyInfo[8]]
            newDailyInfo.append([dailyInfo[-1], str(thisDateObj), str2int(dailyInfo[9]), str2int(dailyInfo[10]), str2int(dailyInfo[11])])
            # if diff < earliestDateSeqThisRegion:
            #     earliestDateSeqThisRegion = diff
            #     earliestDateThisRegion = thisDateObj
        # v.insert(0, earliestDateThisRegion)
        # for dailyInfo in v:
        #     dailyInfo.append(int(dailyInfo[-1]) - earliestDateSeqThisRegion)
        # mainInfo = [v[3], v[7], v[8]]
        # v = [v[-1], v[0], v[9], v[10], v[11]]
        newDailyInfo.insert(0, mainInfo)
        newAllRegions[k] = newDailyInfo

    for key, v in newAllRegions.items():
        for i in range(1, len(v)):
            for k in range(1, len(v) - i):
                if v[k + 1][0] < v[k][0]:
                    v[k], v[k + 1] = v[k + 1], v[k]

    newD = {}
    for key in newAllRegions.keys():
        for i in range(1, len(newAllRegions[key])):
            newAllRegions[key][i].extend(
                [newAllRegions[key][i][2], newAllRegions[key][i][3], newAllRegions[key][i][4]])
            if i >= 2:
                newAllRegions[key][i][5] += newAllRegions[key][i - 1][5]
                newAllRegions[key][i][6] += newAllRegions[key][i - 1][6]
                newAllRegions[key][i][7] += newAllRegions[key][i - 1][7]


        firstConfirmed = -1
        firstRecovered = -1
        firstDead = -1

        confirmed100 = -1
        recovered100 = -1
        dead100 = -1

        confirmed1000 = -1
        recovered1000 = -1
        dead1000 = -1

        confirmed5000 = -1
        recovered5000 = -1
        dead5000 = -1

        confirmed10000 = -1
        recovered10000 = -1
        dead10000 = -1

        confirmed50000 = -1
        recovered50000 = -1
        dead50000 = -1

        confirmed100000 = -1
        recovered100000 = -1
        dead100000 = -1

        for i in range(1, len(newAllRegions[key])):
            newAllRegions[key][i][1] = newAllRegions[key][i][1].replace('2020-','')

            if newAllRegions[key][i][5] > 0 and firstConfirmed == -1:
                firstConfirmed = newAllRegions[key][i][0]
            if newAllRegions[key][i][6] > 0 and firstRecovered == -1:
                firstRecovered = newAllRegions[key][i][0]
            if newAllRegions[key][i][7] > 0 and firstDead == -1:
                firstDead = newAllRegions[key][i][0]

            if newAllRegions[key][i][5] > 100 and confirmed100 == -1:
                confirmed100 = newAllRegions[key][i][0]
            if newAllRegions[key][i][6] > 100 and recovered100 == -1:
                recovered100 = newAllRegions[key][i][0]
            if newAllRegions[key][i][7] > 100 and dead100 == -1:
                dead100 = newAllRegions[key][i][0]

            if newAllRegions[key][i][5] > 1000 and confirmed1000 == -1:
                confirmed1000 = newAllRegions[key][i][0]
            if newAllRegions[key][i][6] > 1000 and recovered1000 == -1:
                recovered1000 = newAllRegions[key][i][0]
            if newAllRegions[key][i][7] > 1000 and dead1000 == -1:
                dead1000 = newAllRegions[key][i][0]
                
            if newAllRegions[key][i][5] > 5000 and confirmed5000 == -1:
                confirmed5000 = newAllRegions[key][i][0]
            if newAllRegions[key][i][6] > 5000 and recovered5000 == -1:
                recovered5000 = newAllRegions[key][i][0]
            if newAllRegions[key][i][7] > 5000 and dead5000 == -1:
                dead5000 = newAllRegions[key][i][0]
            
            if newAllRegions[key][i][5] > 10000 and confirmed10000 == -1:
                confirmed10000 = newAllRegions[key][i][0]
            if newAllRegions[key][i][6] > 10000 and recovered10000 == -1:
                recovered10000 = newAllRegions[key][i][0]
            if newAllRegions[key][i][7] > 10000 and dead10000 == -1:
                dead10000 = newAllRegions[key][i][0]

            if newAllRegions[key][i][5] > 50000 and confirmed50000 == -1:
                confirmed50000 = newAllRegions[key][i][0]
            if newAllRegions[key][i][6] > 50000 and recovered50000 == -1:
                recovered50000 = newAllRegions[key][i][0]
            if newAllRegions[key][i][7] > 50000 and dead50000 == -1:
                dead50000 = newAllRegions[key][i][0]
                
            if newAllRegions[key][i][5] > 100000 and confirmed100000 == -1:
                confirmed100000 = newAllRegions[key][i][0]
            if newAllRegions[key][i][6] > 100000 and recovered100000 == -1:
                recovered100000 = newAllRegions[key][i][0]
            if newAllRegions[key][i][7] > 100000 and dead100000 == -1:
                dead100000 = newAllRegions[key][i][0]
        newDict = {}
        if firstConfirmed != -1 or firstRecovered != -1 or firstDead != -1:
            newDict[1] = [firstConfirmed, firstRecovered, firstDead]
        if confirmed100 != -1 or recovered100 != -1 or dead100 != -1:
            newDict[100] = [confirmed100, recovered100, dead100]
        if confirmed1000 != -1 or recovered1000 != -1 or dead1000 != -1:
            newDict[1000] = [confirmed1000, recovered1000, dead1000]
        if confirmed5000 != -1 or recovered5000 != -1 or dead5000 != -1:
            newDict[5000] = [confirmed5000, recovered5000, dead5000]
        if confirmed10000 != -1 or recovered10000 != -1 or dead10000 != -1:
            newDict[10000] = [confirmed10000, recovered10000, dead10000]
        if confirmed50000 != -1 or recovered50000 != -1 or dead50000 != -1:
            newDict[50000] = [confirmed50000, recovered50000, dead50000]
        if confirmed100000 != -1 or recovered100000 != -1 or dead100000 != -1:
            newDict[100000] = [confirmed100000, recovered100000, dead100000]

        newAllRegions[key][0].extend([newDict])
            # ([{1:[firstConfirmed, firstRecovered, firstDead],
            #                           100:[confirmed100, recovered100, dead100],
            #                           1000:[confirmed1000, recovered1000, dead1000],
            #                           5000:[confirmed5000, recovered5000, dead5000],
            #                           10000:[confirmed10000, recovered10000, dead10000],
            #                           50000:[confirmed50000, recovered50000, dead50000],
            #                           100000:[confirmed100000, recovered100000, dead100000]
            #                            }])
            # ([firstConfirmed, firstRecovered, firstDead,
            #                           confirmed100, recovered100, dead100,
            #                           confirmed1000, recovered1000, dead1000,
            #                           confirmed5000, recovered5000, dead5000,
            #                           confirmed10000, recovered10000, dead10000,
            #                           confirmed50000, recovered50000, dead50000,
            #                           confirmed100000, recovered100000, dead100000])

        # newAllRegions[key]
        # val = ['fuck'] + val[0] + val[firstConfirmed:]
        i = 1
        while i < len(newAllRegions[key]):
            if (newAllRegions[key][i][5] == 0 and newAllRegions[key][i][6] == 0 and newAllRegions[key][i][7] == 0):
                del newAllRegions[key][i]
            else:
                i += 1
    for k, v in newAllRegions.items():
        print(k, v)
    with open('foreign.json','w') as fw:
        fw.write(json.dumps(newAllRegions, separators=(',',':'), ensure_ascii=False))
def str2int(d):
    if d == '':
        return 0
    else:
        return int(d)
if __name__ == '__main__':
    main()